﻿using System;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class ServerResponseEvent : EventNode
    {
        public static Action<string, string, string> OnResponseEvent;
        
        [RequiredField]
        public BBParameter<string> responseName;
        public BBParameter<string> key;
        public BBParameter<string> val;

        private FlowOutput onReceived;
        
        public override string name
        {
            get
            {
                return string.Format("{0} ({1})", "ServerResponseEvent", responseName.value);
            }
        }

        protected override void RegisterPorts()
        {
            OnResponseEvent += OnResponseEventFunc;
            
            onReceived = AddFlowOutput("OnReceived");
        }

        private void OnResponseEventFunc(string responseName, string key, string val)
        {
            if (responseName == this.responseName.value && key == this.key.value && val == this.val.value)
            {
                onReceived.Call(new Flow());
            }
        }
        
        public override void OnPostGraphStoped()
        {
            OnResponseEvent -= OnResponseEventFunc;
        }
    }
}